#!/bin/bash
DIR=`dirname $0`
PWDPATH=`cd $DIR && pwd`

cd $PWDPATH

dirList=("$PWDPATH" "$PWDPATH/vendor" "$PWDPATH/app")

install(){
  echo "*****开始安装依赖包*****"
  # shellcheck disable=SC2068
  for dirPath in ${dirList[@]}
  do
    if [ -d "$dirPath" ]; then
      echo "==>$dirPath"
      cd $dirPath && npm install --legacy-peer-deps
    fi
  done
  echo "*****安装依赖包完成*****"
}

clean() {
  echo "*****开始删除node_modules目录*****"
# shellcheck disable=SC2068
  for dirPath in ${dirList[@]}
  do
    if [ -d "$dirPath/node_modules" ]; then
      echo "$dirPath/node_modules"
      rm -rf $dirPath/node_modules
      rm -rf $dirPath/package-lock.json
    fi
  done

  echo "*****清理node_modules完成!*****"
}

case "$1" in
    install)
        $1
        exit 0
        ;;
    clean)
        $1
        exit 0
        ;;
    *)
        echo $DIR
        echo 'Usage: {install|clean}'
        ;;
esac